//
//  WWHomeViewController.m
//  NTBuildOC
//
//  Created by yuhongdong on 2020/9/13.
//  Copyright © 2020 WeChat. All rights reserved.
//

#import "WWHomeViewController.h"
#import "RadarView.h"
#import "WWButton.h"
#import "LANProperties.h"
#import "MMLANScanner.h"
#import "MMDevice.h"
#import "WWHomeScanCompleteView.h"
#import "WWResultViewController.h"


#define kScanViewWH  SCREEN_WIDTH
#define kScanInnerViewWH 250*ScaleX

@interface WWHomeViewController ()<MMLANScannerDelegate>

@property (nonatomic, strong) RadarView *scanView;
@property (nonatomic, strong) UIImageView *backImageView;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) WWButton *submitButton;
@property (nonatomic, strong) UIButton *leftNavButton;
@property (nonatomic, strong) UIButton *rightNavButton;
@property (nonatomic, strong) UILabel *tipsLabel;
@property (nonatomic, strong) WWHomeScanCompleteView *completeView;

// 扫描器
@property(nonatomic,strong)MMLANScanner *lanScanner;
// 是否是正在扫描中
@property(nonatomic,assign,readwrite)BOOL isScanRunning;
// 设备列表
@property(nonatomic,strong)NSMutableArray *connectedDevices;

@end

@implementation WWHomeViewController
#pragma mark - ♻️life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self startScanAnimation];
    
    self.isScanRunning = NO;
    self.lanScanner = [[MMLANScanner alloc] initWithDelegate:self];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self hideNavigationBar];
}

#pragma mark - 🔄overwrite

#pragma mark - 🚪public
-(void)startScanAnimation{
    [self.scanView startAnimatian];
}

#pragma mark - 🍐delegate
#pragma mark - MMLANScannerDelegate
-(void)lanScanDidFindNewDevice:(MMDevice*)device{
    if (![self.connectedDevices containsObject:device]) {
        [self.connectedDevices addObject:device];
    }
}

-(void)lanScanDidFinishScanningWithStatus:(MMLanScannerStatus)status{
   
    self.isScanRunning=NO;
    
    if (status == MMLanScannerStatusFinished) {
        DEF_DEBUG(@"完成");
        [self scanComplete];
    } else if (status==MMLanScannerStatusCancelled) {
        DEF_DEBUG(@"取消");
    } else {
        DEF_DEBUG(@"其他问题");
    }
}

-(void)lanScanProgressPinged:(float)pingedHosts from:(NSInteger)overallHosts {
    // 进度
    float progressValue = pingedHosts/overallHosts;
    // 改变文字
    self.numberLabel.text = [NSString stringWithFormat:@"%.0f%%",progressValue * 100];
    // 改变提示文字
    self.tipsLabel.text = @"搜寻中...";
}

-(void)lanScanDidFailedToScan {
    self.isScanRunning=NO;
}

#pragma mark - ☎️notification

#pragma mark - 🎬event response
-(void)submitButtonClick{
    DEF_DEBUG(@"扫描按钮点击");
    if ([self.submitButton.titleLabel.text isEqualToString:@"点击查看检测结果"]) {
        WWResultViewController *resultVC = [[WWResultViewController alloc]init];
        [self.navigationController pushViewController:resultVC animated:YES];
        return;
    }

    if (self.isScanRunning) {
        return;
    }else {
        [self startNetworkScan];
    }
    // 初始化扫描
    [self resetScan];
}

-(void)leftNavButtonClick {
    
}

-(void)rightNavButtonClick {
    
}

#pragma mark - 🔒private
-(void)startNetworkScan {
    
    self.isScanRunning=YES;
    
    self.connectedDevices = [[NSMutableArray alloc] init];
    
    [self.lanScanner start];
};

-(void)stopNetworkScan {
    
    [self.lanScanner stop];
    
    self.isScanRunning=NO;
}

// 扫描完成
-(void)scanComplete {
    // 显示扫描完成视图
    self.completeView.hidden = NO;
    // 设置扫描完成的设备数量
    [self.completeView setDeviceNumber:[NSString stringWithFormat:@"%lu",(unsigned long)self.connectedDevices.count]];
    // 显示扫描完成的比例
    self.numberLabel.text = @"100%";
    // 隐藏提示文案
    self.tipsLabel.hidden = YES;
    // 按钮文字改变
    [self.submitButton setTitle:@"点击查看检测结果" forState:UIControlStateNormal];
    // 停止扫描
    [self stopNetworkScan];
}

// 重新扫描
-(void)resetScan {
    // 显示扫描完成视图
    self.completeView.hidden = YES;
    // 设置扫描完成的设备数量
    [self.completeView setDeviceNumber:@"0"];
    // 显示扫描完成的比例
    self.numberLabel.text = @"0%";
    // 初始化提示文案
    self.tipsLabel.hidden = NO;
    self.tipsLabel.text = @"你身边是否潜在偷拍设备？";
    // 按钮文字改变
    [self.submitButton setTitle:@"扫描检测，一键查找隐藏摄像头" forState:UIControlStateNormal];
}


#pragma mark - 🌲setupUI
-(void)setupUI{
    self.view.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:self.backImageView];
    [self.view addSubview:self.scanView];
    [self.view addSubview:self.numberLabel];
    [self.view addSubview:self.submitButton];
    [self.view addSubview:self.leftNavButton];
    [self.view addSubview:self.rightNavButton];
    [self.view addSubview:self.tipsLabel];
    [self.view addSubview:self.completeView];
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.scanView);
        make.width.height.mas_equalTo(280*ScaleX);
    }];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.scanView);
    }];
    
    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-80);
    }];
    
    [self.leftNavButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(37*ScaleX);
    }];
    
    [self.rightNavButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-20);
        make.centerY.equalTo(self.leftNavButton);
    }];
    
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.submitButton.mas_top).offset(-40*ScaleX);
    }];
    
    [self.completeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.submitButton.mas_top).offset(-20*ScaleX);
        make.height.mas_equalTo(100);
    }];
    
}

#pragma mark - 🖼getter and setter
-(RadarView *)scanView{
    if (_scanView == nil) {
        _scanView = [RadarView scanRadarViewWithRadius:kScanInnerViewWH / 2 angle:100 radarLineNum:15 hollowRadius:0];
        _scanView.frame = CGRectMake(0, NAV_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_WIDTH);
        _scanView.startColor = [UIColor colorWithRed:42/255.0 green:255/255.0 blue:213/255.0 alpha:1];
        _scanView.endColor = [UIColor colorWithRed:6/255.0 green:6/255.0 blue:6/255.0 alpha:0];
        _scanView.radarLineColor = [UIColor clearColor];
    }
    return _scanView;
}

-(UIImageView *)backImageView{
    if (_backImageView == nil) {
        _backImageView = [[UIImageView alloc]init];
        _backImageView.image = [UIImage imageNamed:@"leida_back"];
    }
    return _backImageView;
}

-(UILabel *)numberLabel{
    if (_numberLabel == nil) {
        _numberLabel = [[UILabel alloc]init];
        _numberLabel.text = @"0%";
        _numberLabel.textColor = [UIColor colorWithHexString:@"#85FFA5"];
        _numberLabel.font = [UIFont boldSystemFontOfSize:48];
    }
    return _numberLabel;
}

-(WWButton *)submitButton{
    if (_submitButton == nil) {
        _submitButton = [WWButton buttonWithType:UIButtonTypeCustom];
        [_submitButton gradientWithSize:CGSizeMake(260*ScaleX, 40*ScaleX)];
        [_submitButton setTitle:@"扫描检测，一键查找隐藏摄像头" forState:UIControlStateNormal];
        [_submitButton addTarget:self action:@selector(submitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitButton;;
}

-(UIButton *)leftNavButton {
    if (_leftNavButton == nil) {
        _leftNavButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftNavButton setImage:[UIImage imageNamed:@"home_left_nav"] forState:UIControlStateNormal];
        [_leftNavButton addTarget:self action:@selector(leftNavButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftNavButton;
}

-(UIButton *)rightNavButton {
    if (_rightNavButton == nil) {
        _rightNavButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightNavButton setImage:[UIImage imageNamed:@"home_right_nav"] forState:UIControlStateNormal];
        [_rightNavButton addTarget:self action:@selector(rightNavButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightNavButton;;
}

-(UILabel *)tipsLabel {
    if (_tipsLabel == nil) {
        _tipsLabel = [[UILabel alloc]init];
        _tipsLabel.textColor = [UIColor whiteColor];
        _tipsLabel.text = @"你身边是否潜在偷拍设备？";
        _tipsLabel.font = [UIFont systemFontOfSize:13];
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _tipsLabel;
}

-(WWHomeScanCompleteView *)completeView{
    if (_completeView == nil) {
        _completeView = [[WWHomeScanCompleteView alloc]init];
        _completeView.hidden = YES;
    }
    return _completeView;;
}



@end
